package www.openteam.com.adapter.realtime;

import android.content.Context;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;


import java.util.ArrayList;
import java.util.List;


import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.EvalutateAddOrderBean;
import www.openteam.com.bean.MoneryTypeWithRate;
import www.openteam.com.tool.AdpUtil;

public class EvalutateAddOrderAdp extends RecyclerView.Adapter<EvalutateAddOrderAdp.ViewHolder> {

    List<EvalutateAddOrderBean> callBackList = new ArrayList<>();
    Context context;
    private String TAG = this.getClass().getSimpleName();
    List<MoneryTypeWithRate.DataBeanX.DataBean> moneryTypeWithList;
    ArrayAdapter<String> moneryTypeNameAdp;


    public EvalutateAddOrderAdp(Context context) {
        this.context = context;
        EvalutateAddOrderBean bean = new EvalutateAddOrderBean(0, 0, 0, 0);
        callBackList = new ArrayList<>();
        callBackList.add(bean);
    }

    public void setMoneryTypeWithList(List<MoneryTypeWithRate.DataBeanX.DataBean> moneryTypeWithList) {
        this.moneryTypeWithList = moneryTypeWithList;
        List<String> moneryTypeNameList = new ArrayList<>();
        for (MoneryTypeWithRate.DataBeanX.DataBean datum : moneryTypeWithList) {
            moneryTypeNameList.add(datum.getCname());
        }
        moneryTypeNameAdp = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_dropdown_item, moneryTypeNameList);
    }

    public List<EvalutateAddOrderBean> getCallBackList() {
        return callBackList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.evalutate_sum_price, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        EvalutateAddOrderBean addOrderBean = callBackList.get(position);
        EvalutateAddOrderBean bean = new EvalutateAddOrderBean();

        AdpUtil.setEditTextWatcher(holder.evaluateSPNumb, s -> bean.setNumb(stringToInt(s.toString())),
                () -> addOrderBean.getNumb() + "");
//        AdpUtil.setEditTextWatcher(holder.evaluateSPCurrency, s -> bean.setCurrency(stringToInt(s.toString())),
//                ()->addOrderBean.getCurrency()+"");
        AdpUtil.setEditTextWatcher(holder.evaluateSPUnitPrice, s -> bean.setUnitPrice(stringToInt(s.toString())),
                () -> addOrderBean.getUnitPrice() + "");
        AdpUtil.setEditTextWatcher(holder.evaluateSPTotalPrice, s -> bean.setTotalPrice(stringToInt(s.toString())),
                () -> addOrderBean.getTotalPrice() + "");

        if (moneryTypeNameAdp != null) {
            holder.evaluateSPCurrency.setAdapter(moneryTypeNameAdp);
        }

        EvalutateAddOrderBean baseBean = new EvalutateAddOrderBean(0, 0, 0, 0);

        if (position == callBackList.size() - 1) {
            holder.evaluateSPAdd.setVisibility(View.VISIBLE);
        } else {
            holder.evaluateSPAdd.setVisibility(View.GONE);
        }


        holder.evaluateSPAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBackList.size() <= 10) {
                    //最多只能新增10个
                    callBackList.add(position + 1, baseBean);
                    notifyDataSetChanged();
                    notifyItemRemoved(position + 1);
                }
            }
        });
        holder.evaluateSPRemove.setOnClickListener(view -> {
            if (callBackList.size() > 1) {
                //留一个不删除
                callBackList.remove(position);
                notifyDataSetChanged();
                notifyItemRemoved(position);
            }
        });


        holder.evaluateSPUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBackList.get(position) != null) {
                    callBackList.remove(position);
                }
                callBackList.add(position, bean);
            }
        });


    }


    public static int stringToInt(String s) {
        return Integer.valueOf(s.isEmpty() ? "0" : s);
    }

    @Override
    public int getItemCount() {
        return callBackList == null ? 0 : callBackList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.evaluate_SP_numb)
        EditText evaluateSPNumb;
        @Bind(R.id.evaluate_SP_currency)
        AppCompatSpinner evaluateSPCurrency;
        @Bind(R.id.evaluate_SP_unitPrice)
        EditText evaluateSPUnitPrice;
        @Bind(R.id.evaluate_SP_totalPrice)
        EditText evaluateSPTotalPrice;
        @Bind(R.id.evaluate_SP_add)
        Button evaluateSPAdd;
        @Bind(R.id.evaluate_SP_remove)
        Button evaluateSPRemove;
        @Bind(R.id.evaluate_SP_update)
        Button evaluateSPUpdate;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
